package com.wmh.baseservice.operationaid.controller.store;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.operationaid.entity.PddStoreData;
import com.wmh.baseservice.operationaid.entity.view.PddStoreDataView;
import com.wmh.baseservice.operationaid.pojo.vo.store.PddStoreListReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.store.PddStorePageReqVO;
import com.wmh.baseservice.operationaid.service.PddStoreDataService;
import com.wmh.baseservice.operationaid.service.PddStoreDataViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;
import static org.springframework.util.CollectionUtils.isEmpty;

@Validated
@RestController
@RequestMapping("store/pdd")
@Api(tags = "店铺数据-拼多多")
@Slf4j
public class PddStoreController {
    @Resource
    private PddStoreDataService pddStoreDataService;

    @Resource
    private PddStoreDataViewService pddStoreDataViewService;

    @ApiOperation("获取拼多多店铺选项列表")
    @PostMapping("storeOptions")
    public String getStoreOptions(){
        return success(pddStoreDataService.getStoreOptions());
    }

    @ApiOperation(value = "获取拼多多店铺列表")
    @PostMapping("/list")
    public String list(PddStoreListReqVO reqVO) {
        return success(pddStoreDataViewService.getPddStoreDataViewList(reqVO));
    }

    @ApiOperation(value = "获取拼多多店铺分页列表")
    @PostMapping("/page")
    public String page(@Validated PddStorePageReqVO reqVO) {
        PageResult<PddStoreDataView> pageResult = new PageUtil<PddStoreDataView>(reqVO).startPage((page, wrapper) -> {

            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), PddStoreDataView.COL_STORE_NAME, reqVO.getStoreName());

            List<String> dates = reqVO.getDates();

            if (!isEmpty(dates)) {
                List<LocalDateTime> dateTimes = dates.stream()
                        .map(date -> LocalDateTimeUtil.parse(date + "T00:00:00"))
                        .collect(Collectors.toList());
                wrapper.in(PddStoreData.COL_DATA_DATE, dateTimes);
            }
            pddStoreDataViewService.page(page, wrapper);
        });

        log.info("pageResult {}", pageResult);

        return success(pageResult);
    }

    @ApiOperation(value = "批量移除拼多多店铺列表")
    @PostMapping("/del")
    public String batchRemove(@Size(min = 3, message = "请上传主键") @NotBlank(message = "请上传主键") String ids) {
        pddStoreDataService.batchRemove(JSON.parseArray(ids, String.class));
        return ReturnBody.success();
    }

    @ApiOperation(value = "下载拼多多店铺数据为 Excel 表格")
    @PostMapping("/download")
    public void downloadExcel(HttpServletResponse response, PddStoreListReqVO reqVO) {
        pddStoreDataViewService.writeExcel(response, reqVO);
    }

}
